'use strict';

const xtpl = require('xtpl');
const path = require('path');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));
const fs = require('fs');

module.exports.classsList = function(req,res)
{
	//判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 3;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('class').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('class').alias('class').join({
            table: 'school',
            join: 'left',
            as: 'school',
            on: ['school_id', 'school_id']
        }).where(where).order('class.class_id DESC').limit(start,limit).select().then(function(class_list){

            let data = {
                array:class_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"classsList.html",data);
        });


    });

};

module.exports.classsEdit = function(req,res)
{
    let class_id = req.query.class_id;

    if(!class_id)
    {
        //重定向
        res.redirect(`/classs/classsList`);
        return false;
    }

    // db.table('class').alias('classs').join({
    //     table: 'school',
    //     join: 'left',
    //     as: 'school',
    //     on: ['school_id', 'school_id']
    // }).where(`classs.class_id = ${class_id}`).find().then(function(classId){
    //     let data = {
    //         array:classId,
    //     };
    //     CommonHelper.renderFile(req,res,"classsEdit.html",data);
    // });


    let where = 1;
    db.table('class').where(`class_id = ${class_id}`).find().then(function(class_Id){
        db.table('school').where(where).select().then(function(affectschoolId){
            let data = {
                "class":class_Id,
                "school_list":affectschoolId
            }
            CommonHelper.renderFile(req,res,"classsEdit.html",data);
        })

    })

}

module.exports.classEditForm = function(req,res)
{
    let class_id = req.query.class_id;

    if(!class_id)
    {
        //重定向
        res.redirect(`/classs/classsEdit`);
        return false;
    }

    db.table('class').where(`class_id = ${class_id}`).find().then(function(classId){

        let data = {
            "class_name":req.body.class_name,
            "school_id":req.body.school_id
        }
        db.table('class').where(`class_id = ${class_id}`).update(data).then(function(affectId){

            if(affectId)
            {
                //重定向
                res.redirect(`/classs/classsList`);
                return false;
            }else{
                //重定向
                res.redirect(`/classs/classsEdit?class_id=${class_id}`);
                return false;
            }
        });

    });
}

module.exports.classsAdd = function(req,res)
{
    db.table('school').select().then(function(school){
        let Data = {
            "school_list":school
        };
        CommonHelper.renderFile(req,res,"classsAdd.html",Data);

    });
}

module.exports.classsAddForm = function(req,res)
{
    let class_name = req.body.class_name;
    let school_id = req.body.school_id;
    db.table('class').where(`class_name = '${class_name}' AND school_id = '${school_id}'`).find().then(function(data){

        if(JSON.stringify(data) != "{}")
        {
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('该学校已有该班级');location.href='/classs/classsAdd';</script>");
        }else{

            let data = {
                "class_name":req.body.class_name,
                "school_id":req.body.school_id
            }
            db.table('class').add(data).then(function(inclasstId){
                if(inclasstId)
            {
                //重定向
                res.redirect(`/classs/classsList`);
                return false;
            }else{
                //重定向
                res.redirect(`/classs/classsAdd`);
                return false;
            }
            })
        }
    });
}

module.exports.classsDelete = function(req,res)
{
    let delete_id = req.query.delete_id;

    db.table('class').where(`class_id = ${delete_id}`).delete().then(function(res_class){
        if(res_class)
        {
            res.redirect(`/classs/classsList`);
            return false;
        }else{
           res.redirect(`/classs/classsList`);
            return false;
        }
    })
}